package com.itfenghuang.day15;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class A05_HTTPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(7707);
        BufferedReader br = null;
        FileInputStream fis = null;
        while (true) {
//       监听连接
            Socket at = ss.accept();
//        获取输入流
            InputStream is = at.getInputStream();
//        读取数据
//        byte[] bys = new byte[1024];
//        int len;
//        while ((len=is.read(bys))!=-1){
//            System.out.println(new String(bys,0,len));
//        }
            br = new BufferedReader(new InputStreamReader(is));
//        读取第一行
            String line = br.readLine();
            System.out.println(line);
            String[] s = line.split(" ");
            String addr=s[1];
             addr = addr.substring(1);
//        使用本地字节流
            fis = new FileInputStream(addr);
            OutputStream os = at.getOutputStream();
//        标准写法   写入HTTP协议的响应头
            os.write("HTTP/1.1 200 OK\r\n".getBytes());
            os.write("Content-Type:text/html\r\n".getBytes());
//      写一个换行符
            os.write("\r\n".getBytes());
//        循环读写
            byte[] bys = new byte[1024];
            int len;
            while ((len=fis.read(bys))!=-1){
                os.write(bys,0,len);
            }
//        释放资源
        fis.close();
        br.close();
//        ss.close();
        }

    }
}
